﻿// Open Source: MIT License (http://www.opensource.org/licenses/mit-license.php)
// Project: YoProgramo.NET (http://code.google.com/p/yoprogramo/)
// Autor: Fabian Figueredo (info@fabianfigueredo.com.ar)
// Blog: blog.yoprogramo.net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Castle.Windsor;
using log4net;

namespace YoProgramo.Infrastructure.Tools.Windsor
{
    public class WindsorControllerFactory : IControllerFactory {
        private IWindsorContainer _container;
        private static readonly ILog log = LogManager.GetLogger(typeof(WindsorControllerFactory));

        public WindsorControllerFactory(IWindsorContainer c)
        {
            _container = c;
        }

        public IController CreateController(RequestContext requestContext, string controllerName)
        {
            try
            {
                if (!controllerName.ToLower().Contains("controller"))
                    controllerName += "Controller";

                return (IController)_container.Resolve<IController>(controllerName);
            }
            catch(Exception ex)
            {
                log.Info("Controlador no encontrado en el contenedor");
                HttpContext.Current.Response.Redirect("/Error/Unknown/");
            }
            return (IController)_container.Resolve<IController>("HomeController");
        }

        public void ReleaseController(IController controller)
        {
            
        }
    }
}